package seqlist.leetcode;

import javax.xml.soap.Node;

public class leetcode203 {
    public ListNode203 removeElements2(ListNode203 head, int val) {
        ListNode203 dummyhead=new ListNode203();
        dummyhead.next=head;
        ListNode203 prev=dummyhead;
        while (prev.next!=null){
            if (prev.next.val==val){
                ListNode203 node =prev.next;
                prev.next=node.next;
            }else {
                prev=prev.next;
            }
        }
        return dummyhead.next;
    }









    public ListNode203 removeElements(ListNode203 head, int val) {
        while (head != null && head.val == val) {
            head = head.next;
        }
        if (head == null) {
            return head;
        } else {
            ListNode203 prev = head;
            while (prev.next != null) {
                if (prev.next.val == val) {
                    ListNode203 node = prev.next;
                    prev.next = node.next;
                } else {
                    prev = prev.next;
                }
            }
            return head;
        }
    }

    public ListNode203 removeElements1(ListNode203 head, int val) {
        if (head==null){
            return null;
        }
        //第二个节点开始交给子函数去处理
        head.next=removeElements(head.next,val);
//        if (head.val==val){
//            return head.next;
//        }
//        return head;
        return head.val==val?head.next:head;
    }
}
